// 有n个人，一个数组表示耐受度，k表示数几个，数到那人耐受度减一，当为0时离场
// 按顺序输出从早到晚每个人的编号  数组方式返回

function solution(n, k, a) {
  let res = []
  let queue = []  // shift push

  for (let i = 0; i < n; i++) {
    queue.push({
      id: i + 1,
      tol: a[i]
    })
  }

  let currentIndex = 0


  while (queue.length) {
    currentIndex = (currentIndex + k - 1) % queue.length
    queue[currentIndex].tol -= 1
    if (queue[currentIndex].tol === 0) {
      res.push(queue[currentIndex].id)
      queue.splice(currentIndex, 1)
    } else {
      currentIndex = (currentIndex + 1) % queue.length
    }

  }

  return res;
}


function main() {
  console.log(JSON.stringify(solution(5, 2, [1, 4, 2, 9, 4])));
  console.log(JSON.stringify(solution(3, 2, [1, 2, 3])) === JSON.stringify([1, 3, 2]));
  console.log(JSON.stringify(solution(4, 3, [3, 1, 2, 1])) === JSON.stringify([2, 1, 3, 4]));
  console.log(JSON.stringify(solution(5, 1, [2, 2, 3, 1, 2])) === JSON.stringify([4, 1, 2, 5, 3]));
}

main();